/**
 * Created by david on 10/22/13.
 */
window.VoteView = Parse.View.extend({
    el: 'divVote',
    events: {},
    initialize: function() {
        _.bindAll(this, 'render', 'paginate');
    },
    render: function() {
        this.paginate();
    },
    paginate: function(page) {
        $('#tbl-vote tr:gt(0)').remove();
        $('.pagination').html('');

        if(page == null || page <= 0){
            page = 1;
        }

        var limit = 9;
        var offset = limit * (page-1);

        //find the vote
        var Vote = Parse.Object.extend("Vote");
        var query = new Parse.Query(Vote);
        //pagination
        query.count({
            success: function(count) {
                var totalPage = Math.ceil(count / limit);

                // The count request succeeded. Show the count
                for (i = 1; i <= totalPage; i++) {
                    $('.pagination').append('<a href="#page/'+i+'">' + ' ' + i + ' ' +'</span>');
                }
            },
            error: function(error) {
                // The request failed
            }
        });

        query.limit(limit);
        query.skip(offset);
        query.descending("createdAt");
        query.find({
            success: function(results) {
                _.defer(function() {
                    $('.loading').hide();
                });

                _.each(results, function(vote) {
                    var peopleObj = vote.get('people');
                    if (peopleObj != 'undefined') {
                        var people = Parse.Object.extend("People");
                        var peopleQuery = new Parse.Query(people);
                        peopleQuery.get(peopleObj.id, {
                            success: function(returnPeople) {
                                // The object was retrieved succesfully.
                                vote.set('people', returnPeople);
                                var date = dateFormat(new Date(vote.createdAt), 'dd/mm/yyyy HH:MM');

                                //find user
                                var userObj = vote.get('user');

                                if (userObj != null) {
                                    var user = Parse.Object.extend('User');
                                    var userQuery = new Parse.Query(user);
                                    userQuery.get(userObj.id, {
                                        success: function(returnUser) {
                                            //
                                            var html = '<tr>\n\
                                                        <td>' + date + '</td>\n\
                                                        <td>' + returnPeople.get('peopleName') + '</td>\n\
                                                        <td>' + vote.get('currency')+vote.get('amount') + '</td>\n\
                                                        <td>' + returnUser.get('username') + '</td>\n\
                                                        <td>' + vote.get('comment') + '</td>\n\
                                                    </tr>';
                                            $('#tbl-vote').append(html);
                                        }
                                    });
                                } else {
                                    //
                                    var html = '<tr>\n\
                                                <td>' + date + '</td>\n\
                                                <td>' + returnPeople.get('peopleName') + '</td>\n\
                                                <td>' + vote.get('currency')+vote.get('amount')  + '</td>\n\
                                                <td>' + 'Anonymous' + '</td>\n\
                                                <td>' + vote.get('comment') + '</td>\n\
                                            </tr>';
                                    $('#tbl-vote').append(html);
                                }


                            },
                            error: function(object, error) {
                                // The object was not retrieved successfully.
                                console.log(error)
                            }
                        });
                    }
                });
            },
            error: function(error) {
                alert("Error: " + error.code + " " + error.message);
            }
        });
    }
});



var AppRouter = Parse.Router.extend({
    routes: {
        "": "index",
        'page/:page': 'index'
    },
    initialize: function() {
        this.voteView = new VoteView();
    },
    //index action
    index: function(page) {
        if(page == null){
            this.voteView.render();
        }else{
            this.voteView.paginate(page);
        }
    }

});

app = new AppRouter();
Parse.history.start();